﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EPiServer.Personalization.VisitorGroups;
using EPiServer.Data;

namespace CriteriaPack.BrowserOSCriterion
{    
    public class OSBrowserModel : CriterionModelBase
    {
        #region Parameters
        private string _operating;
        private string _browser;
        private MatchStringType _browserMatchType;
        private MatchStringType _osMatchType;
        private EPiServer.Data.Identity _id;
        #endregion

        /// <summary>
        /// Gets or sets the OS name.
        /// </summary>
        /// <value>OS name</value>
        public string OperatingSystem { get { return _operating; } set { this._operating = value; } }

        /// <summary>
        /// Gets or sets the browser name.
        /// </summary>
        /// <value>browser name</value>
        public string Browser { get { return _browser; } set { this._browser = value; } }

        /// <summary>
        /// Gets or sets the browser name match type.
        /// </summary>
        /// <value>browser name match type.</value>
        [DojoWidget(SelectionFactoryType = typeof(EPiServer.Web.Mvc.VisitorGroups.EnumSelectionFactory), AdditionalOptions = "{ selectOnClick: true }")]
        public MatchStringType BrowserMatchType
        {
            get
            {
                return this._browserMatchType;
            }
            set
            {
                this._browserMatchType = value;
            }
        }

        /// <summary>
        /// Gets or sets the platform name match type.
        /// </summary>
        /// <value>platform name match type.</value>
        [DojoWidget(SelectionFactoryType = typeof(EPiServer.Web.Mvc.VisitorGroups.EnumSelectionFactory), AdditionalOptions = "{ selectOnClick: true }")]
        public MatchStringType OSMatchType
        {
            get
            {
                return this._osMatchType;
            }

            set
            {
                this._osMatchType = value;
            }
        }



        public override ICriterionModel Copy()
        {
            return base.ShallowCopy();
        }
    }

    [VisitorGroupCriterion(
        Category = "Technical Criteria",
        Description = "Match Operating sytem & Browser name with specified value",
        DisplayName = "OS & Browser")]
    public class OSBrowserCriterition : CriterionBase<OSBrowserModel>
    {
        private string _operatingSystem;
        private string _browser;

        public override bool IsMatch(System.Security.Principal.IPrincipal principal, System.Web.HttpContextBase httpContext)
        {
            return ((String.IsNullOrEmpty(base.Model.OperatingSystem) ? true : StringMatchHelper.IsMatch(_operatingSystem, base.Model.OperatingSystem, base.Model.OSMatchType))
                && (String.IsNullOrEmpty(base.Model.Browser) ? true : StringMatchHelper.IsMatch(_browser, base.Model.Browser, base.Model.BrowserMatchType)));             
        }

        public override void Subscribe(ICriterionEvents criterionEvents)
        {
            base.Subscribe(criterionEvents);
            criterionEvents.StartRequest += criterionEvents_StartRequest;
        }

        private void criterionEvents_StartRequest(object sender, CriterionEventArgs e)
        {
            _browser = e.HttpContext.Request.Browser.Browser;
            _operatingSystem = e.HttpContext.Request.Browser.Platform;
        }

        public override void Unsubscribe(ICriterionEvents criterionEvents)
        {
            criterionEvents.StartRequest -= criterionEvents_StartRequest;
            base.Unsubscribe(criterionEvents);
        }
    }
}
